require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")



namespace :juice do
	desc "Parse a production log file and import into database, specifing with LOG_FILE=/path/to/production.log " + 
    "and PARAMS_TO_SKIP=password,secret (and you probably want RAILS_ENV=production)"
	task :import do
    if ENV['LOG_FILE']
      require File.join(File.dirname(__FILE__), "../log_parser.rb")
			params_to_skip = ENV['PARAMS_TO_SKIP'] ? ENV['PARAMS_TO_SKIP'].split(",").map(&:strip) : nil
			LogParser.do_parse ENV['LOG_FILE'], :skip_params => params_to_skip
      
      unless ENV['RAILS_ENV'] == 'test'
        # clean out page and action caches after loading new stuff
        puts "clearing cached pages, actions, and fragments..."
        Rake::Task["tmp:cache:clear"].invoke
        FileUtils.rm_r(Dir.glob("public/logged_controllers/*")) rescue Errno::ENOENT
        FileUtils.rm(Dir.glob("public/logged_controllers.html")) rescue Errno::ENOENT
        FileUtils.rm(Dir.glob("public/index.html")) rescue Errno::ENOENT
      end
		else
			puts "specify log file with LOG_FILE=/path/to/production.log"
		end
	end

	desc "Parse a production log file without importing (test run), specifying LOG_FILE=/path/to/production.log " + 
    "and PARAMS_TO_SKIP=password,secret (and you probably want RAILS_ENV=production)"
	task :read_test do
		if ENV['LOG_FILE']
			require File.join(File.dirname(__FILE__), "../log_parser.rb")
			params_to_skip = ENV['PARAMS_TO_SKIP'] ? ENV['PARAMS_TO_SKIP'].split(",").map(&:strip) : nil
			LogParser.do_parse ENV['LOG_FILE'], :read_only => true, :skip_params => params_to_skip
		else
			puts "specify log file with LOG_FILE=/path/to/production.log"
		end
	end
end
